﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_EditInvite : System.Web.UI.Page
{
    private void FetchKind()
    {
        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Server.MapPath("~/Data/Course.xml");
        xds.XPath = "/Root/Kind";
        Kind.DataSource = xds;
        Kind.DataTextField = "name";
        Kind.DataValueField = "name";
        Kind.DataBind();

        Kind.Items.Insert(0, new ListItem("请选择", "请选择"));

        Subject.Items.Insert(0, new ListItem("请选择", "请选择"));
        Course.Items.Insert(0, new ListItem("请选择", "请选择"));
    }
    private void FetchSubject(string kind)
    {
        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Server.MapPath("~/Data/Course.xml");
        xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject", kind);
        Subject.DataSource = xds;
        Subject.DataTextField = "name";
        Subject.DataValueField = "name";
        Subject.DataBind();

        Subject.Items.Insert(0, new ListItem("请选择", "请选择"));

        Course.Items.Clear();
        Course.Items.Insert(0, new ListItem("请选择", "请选择"));

        Teachers.Items.Clear();
    }
    private void FetchCourse(string kind, string subject)
    {
        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Server.MapPath("~/Data/Course.xml");
        xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject[@name='{1}']/Course", kind, subject);
        Course.DataSource = xds;
        Course.DataTextField = "name";
        Course.DataValueField = "name";
        Course.DataBind();

        Course.Items.Insert(0, new ListItem("请选择", "请选择"));

        Teachers.Items.Clear();
    }
    private void FetchTeachers(string course)
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aT in db.Teacher
                where aT.Verfied != null && aT.Courses.Contains(course)
                select new { ID = aT.ID, Name = aT.Name + "（" + aT.District + "）" };

        Teachers.DataSource = q;
        Teachers.DataTextField = "Name";
        Teachers.DataValueField = "ID";
        Teachers.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FetchKind();

            if (Request.Params["iid"] != null)
            {
                InviteID.Value = Request.Params["iid"];

                CmsDataContext db = new CmsDataContext();
                var q = from aI in db.Invite
                        where aI.ID == InviteID.Value
                        select aI;

                if (q.Count() > 0)
                {
                    Invite inv = q.First();

                    InviteTitle.Text = inv.Title;
                    Memo.Text = inv.Memo;
                    Kind.SelectedValue = inv.Kind;

                    FetchSubject(inv.Kind);

                    Subject.SelectedValue = inv.Subject;

                    FetchCourse(inv.Kind, inv.Subject);

                    Course.SelectedValue = inv.Course;

                    FetchTeachers(inv.Course);

                    Teachers.SelectedValue = inv.TeacherID;
                }
            }
        }
    }
    protected void Kind_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchSubject(Kind.SelectedValue);
    }
    protected void Subject_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchCourse(Kind.SelectedValue, Subject.SelectedValue);
    }
    protected void Course_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchTeachers(Course.SelectedValue);
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string iid = InviteID.Value.Trim();
        CmsDataContext db = new CmsDataContext();
        Invite inv;

        try
        {
            if (iid == "")
            {
                inv = new Invite();
                inv.ID = Toolkit.NextUUID;
                inv.Invited = DateTime.Now;
                inv.InvitorID = Convert.ToString(Session["ManagerID"]);
                inv.District = Toolkit.CurrentUserDistrict;
                inv.Belong = Toolkit.CurrentUserBelong;
                
                db.Invite.InsertOnSubmit(inv);
            }
            else
            {
                inv = (from aI in db.Invite
                       where aI.ID == iid
                       select aI).First();
            }

            inv.Title = InviteTitle.Text.Trim();
            inv.Memo = Memo.Text.Trim();
            inv.Kind = Kind.SelectedValue;
            inv.Subject = Subject.SelectedValue;
            inv.Course = Course.SelectedValue;
            inv.TeacherID = Teachers.SelectedValue;

            db.SubmitChanges();

            MessageBox.Message = "邀请信息已经发出。";
            MessageBox.Location = "./EditInvite.aspx?iid=" + inv.ID;
        }
        catch(Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }
}